﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PostSharp.Aspects;
using ActivitiesControl.Domain.Model;
using ActivitiesControl.Domain.Utils;
using ActivitiesControl.Domain.Model.VOs;

namespace ActivitiesControl.Domain.Services.Impls
{
	[Serializable]
	internal class ExceptionMessageHandlerAttribute
		: OnExceptionAspect
	{
		public override void OnException(MethodExecutionArgs args)
		{
			if (args.Exception is DomainException)
			{
				BaseService raiser = args.Instance as BaseService;

				if (raiser != null)
				{
					raiser.RaiseMessage(
						raiser, 
						new MessageEventArgs 
						{ 
							Message = new Message(
								"Activities Control", 
								args.Exception.Message, 
								MessageType.Error) 
						});

					args.FlowBehavior = FlowBehavior.RethrowException;
				}
			}
		}
	}
}
